# @Time:   2021/2/4 14:39
# @Author: Zong Wei
import random
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class PortPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='海运数据']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='港口管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")

    code = (By.ID, "SeaportCode")
    port_cn = (By.ID, "SeaportName")
    port_en = (By.ID, "SeaportEname")
    country = (By.ID, "CountryCode")
    city = (By.ID, "CityCode")
    province = (By.ID, "ProvinceCode")
    uno = (By.ID, "UnoCode")
    currency = (By.ID, "CurrCode")
    is_active = (By.XPATH, "//select[@name='IsActive']")
    option1 = (By.XPATH, "//option[@value='true']")

    port_type = (By.XPATH, "//div[@id='PortType']/div[contains(.,'请选择')]")
    port_value = (By.XPATH, "//*[@id='PortType-option']//li[2]")
    ln = (By.XPATH, "//form[@id='form1']/div//span/a")
    ln_values = (By.XPATH, "//form[@id='form1']/following::tr[position()>1]")

    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='城市代码']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def port_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_port(self, code='SG', cn='新加坡+', en='Singapore+', country='CN', city='SH', province='PU DONG', uno='T8888888'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加港口")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.code)).send_keys(code)
        self.wait.until(ec.presence_of_element_located(self.port_cn)).send_keys(cn)
        self.wait.until(ec.presence_of_element_located(self.port_en)).send_keys(en)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.country)).send_keys(country)
        self.wait.until(ec.presence_of_element_located(self.city)).send_keys(city)
        self.wait.until(ec.presence_of_element_located(self.province)).send_keys(province)
        self.wait.until(ec.presence_of_element_located(self.uno)).send_keys(uno)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.currency)).send_keys('USD')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.is_active)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.option1)).click()

        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.port_type)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.port_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.ln)).click()
        sleep(1)
        random.choice(self.driver.find_elements(*self.ln_values)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_port(self,  code='SG', cn='新加坡+', en='Singapore+', country='CN'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.code)).send_keys(code)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.port_cn)).send_keys(cn)
        self.wait.until(ec.presence_of_element_located(self.port_en)).send_keys(en)
        self.wait.until(ec.presence_of_element_located(self.country)).send_keys(country)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.port_type)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.port_value)).click()
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.logger.info("查询完成")

    def edit_port(self):
        self.search_port()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.code)).send_keys(' Edit')
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.port_cn)).send_keys(' Edit')
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_port(self):
        self.search_port()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    lp = PortPage()
    lp.port_home()
    lp.del_port()
